package com.questions.helper.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

import com.questions.helper.application.QuestionsHelperApplication;
import com.questions.helper.model.Answer;
import com.questions.helper.model.Questions;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class LegalKnowledgeSQLite {
    private LegalknowledgeSQLiteOpenHelper db;
    Context context;
    public LegalKnowledgeSQLite(Context context) {
        this.context = context;
        db  = new LegalknowledgeSQLiteOpenHelper(context);
    }


    public void saveDate( List<Questions> questionsList){
        SQLiteDatabase sqLiteDatabase = db.getWritableDatabase();
        for(Questions q:questionsList){
            ContentValues contentValues = new ContentValues();
            contentValues.put("title_text",q.getTitleText());
            contentValues.put("answers_sort",q.getAnswersSort());
            contentValues.put("questions_type",q.getQuestionsType());
            long id = sqLiteDatabase.insert(LegalknowledgeSQLiteOpenHelper.tableQuestionsName,null ,contentValues);
            if(id == -1){
                Log.i("插入失败",q.getTitleText());
                throw new RuntimeException("插入失败"+q.getTitleText());
            }
            for (Answer answer:q.getAnswers()){
                ContentValues contentValues_= new ContentValues();
                contentValues_.put("title_text",answer.getTitleText());
                contentValues_.put("answers_sort",answer.getAnswerSort());
                contentValues_.put("answers_order",answer.getAnswerOrder());
                contentValues_.put("answers_correct",answer.getCorrect());
                contentValues_.put("questions_id",id);
                sqLiteDatabase.insert(LegalknowledgeSQLiteOpenHelper.tableAnswerName,null ,contentValues_);
            }
        }
    }

    @SuppressLint("Range")
    public Questions getOneQuestions(String titleText){
        SQLiteDatabase sqLiteDatabase = db.getReadableDatabase();
        @SuppressLint("Recycle")
        Cursor cursor=sqLiteDatabase.rawQuery("select * from "+LegalknowledgeSQLiteOpenHelper.tableQuestionsName+" where title_text=?",new String[]{titleText});
        if(cursor.moveToFirst()){
            Questions questions = new Questions();
            questions.setId(cursor.getLong(cursor.getColumnIndex("id")));
            questions.setTitleText(cursor.getString(cursor.getColumnIndex("title_text")));
            questions.setQuestionsType(cursor.getString(cursor.getColumnIndex("questions_type")));
            questions.setAnswersSort(cursor.getString(cursor.getColumnIndex("answers_sort")));
            List<Answer> answers = new ArrayList<>();
            @SuppressLint("Recycle")
            Cursor cursor_=sqLiteDatabase.rawQuery("select * from "+LegalknowledgeSQLiteOpenHelper.tableAnswerName+" where questions_id=? and answers_correct="+Answer.CORRECT_YES +" order by answers_order asc",new String[]{String.valueOf(questions.getId())});
            while (cursor_.moveToNext()){
                Answer answer = new Answer();
                answer.setId(cursor_.getLong(cursor_.getColumnIndex("id")));
                answer.setTitleText(cursor_.getString(cursor_.getColumnIndex("title_text")));
                answer.setQuestionsId(questions.getId());
                answer.setAnswerOrder(cursor_.getInt(cursor_.getColumnIndex("answers_order")));
                answer.setAnswerSort(cursor_.getString(cursor_.getColumnIndex("answers_sort")));

                answers.add(answer);
            }
            questions.setAnswers(answers);
            return questions;
        }
        return  null;
    }

    public void delFile() {
        try {
            this.db.deleteDabe();
            this.db = new LegalknowledgeSQLiteOpenHelper(context);
        }catch (Exception ignored){}

    }
}
